import os
from win32com import client
from docx import Document

tt_filename = "tt.docx"     # 存放所有记录信息
tt_doc_filepath = "F:/projects/adenoid_demo_new/" + tt_filename
temp_filename = "temp.docx"     # 临时存放需要打印的记录
temp_doc_filepath = "F:/projects/adenoid_demo_new/" + temp_filename

def create_word(infomation):
    # document = Document("F:\\laibo\\Project\\hello.docx")
    tt_document = Document(tt_doc_filepath)
    temp_document = Document(temp_doc_filepath)
    for paragraph in temp_document.paragraphs:
        p = paragraph._element
        p.getparent().remove(p)
        paragraph._p = paragraph._element = None
    # paragraph = document.add_paragraph('你好！打印机！')
    tt_document.add_paragraph(infomation)
    temp_document.add_paragraph(infomation)
    # prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum')
    # document.save(doc_filepath )
    tt_document.save("tt.docx")
    temp_document.save("temp.docx")

def file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        print(root)  # 当前目录路径
        print(dirs)  # 当前路径下所有子目录
        print(files)  # 当前路径下所有非目录子文件
        return files


def print_word(word_file_path):
    # 指示系统中文档的处理工具`
    # 指示运行的版本，如果是WPS应修改为
    # 如果使用word
    exec_tool = 'Word.Application'
    # 如果使用wps
    # exec_tool = 'wps.application'
    word = client.Dispatch(exec_tool)
    # 在后台运行程序
    word.Visible = 0  # 后台运行，不显示
    # 运行过程不警告
    word.DisplayAlerts = 0  # 不警告
    # 打开word文档
    doc = word.Documents.Open(word_file_path)
    # 进行打印
    doc.PrintOut()
    # --------------------------------------------------------
    # -- 最后操作保存
    # --------------------------------------------------------
    # 关闭文件
    doc.Close()
    # 退出word
    word.Quit()


if __name__ == '__main__': 
    create_word("你好，打印机！")
    # print_word(doc_filepath)
    
    
    